package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.PurchaseService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/purchaseListGoods")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    /**
     * 进货单保存
     * @param purchaseList
     * @return
     */
    @PostMapping("/save")
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr){

        List<PurchaseListGoods> goods = new Gson().fromJson(purchaseListGoodsStr, new TypeToken<List<PurchaseListGoods>>() {}.getType());

        for (PurchaseListGoods good : goods) {
            purchaseService.savePurchase(purchaseList,good);
        }

       return new ServiceVO(100, "请求成功");
    }

}
